package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.demo.dto.UserStatsResponse;
import com.example.demo.entity.Exercise;
import com.example.demo.entity.Question;
import com.example.demo.entity.UserAnswer;
import com.example.demo.entity.UserStats;
import com.example.demo.mapper.ExerciseMapper;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.UserAnswerMapper;
import com.example.demo.mapper.UserStatsMapper;
import com.example.demo.service.ExerciseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Service
public class ExerciseServiceImpl extends ServiceImpl<ExerciseMapper, Exercise> implements ExerciseService {

    @Autowired
    private ExerciseMapper exerciseMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserAnswerMapper userAnswerMapper;
    @Autowired
    private UserStatsMapper userStatsMapper;

    @Override
    public List<Exercise> getExercisesBySubject(String subject) {
        return exerciseMapper.selectBySubject(subject);
    }

    @Override
    public List<Question> getQuestionsByExerciseId(int exerciseId) {
        return questionMapper.selectList(null);
    }

    @Override
    public void submitAnswers(List<UserAnswer> userAnswers) {
        userAnswerMapper.insertBatch(userAnswers);
    }

    @Override
    public UserStatsResponse getUserStats(int userId) {
        UserStats userStats = userStatsMapper.selectByUserId(userId);
        UserStatsResponse response = new UserStatsResponse();
        if (userStats != null) {
            response.setTotalExercises(userStats.getTotalExercises());
            response.setAverageScore(userStats.getAverageScore());
            response.setStreak(userStats.getStreak());
        } else {
            // 如果 userStats 为 null，可以根据业务需求设置默认值或返回特定的响应
            response.setTotalExercises(null);
            response.setAverageScore(null);
            response.setStreak(null);
        }
        return response;
    }

    @Override
    public void updateUserStats(int userId, int score) {
        UserStats userStats = Optional.ofNullable(userStatsMapper.selectById(userId)).orElse(new UserStats());
        userStats.setUserId(userId);
        int totalExercises = userStats.getTotalExercises() == null ? 1 : Integer.parseInt(userStats.getTotalExercises()) + 1;
        userStats.setTotalExercises(String.valueOf(totalExercises));
        double totalScore = userStats.getAverageScore() == null ? 0 : Double.parseDouble(userStats.getAverageScore()) * (totalExercises - 1);
        userStats.setAverageScore(String.valueOf((totalScore + score) / totalExercises));
        userStatsMapper.insertOrUpdate(userStats);
    }

    @Override
    public List<Question> getQuestionsBySubject(String subject) {
        return questionMapper.selectQuestionsBySubject(subject);
    }
}